package ar.fi.uba.fallas2.business;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import ar.fi.uba.fallas2.business.factories.AnswerFactory;
import ar.fi.uba.fallas2.business.factories.AnswerKindFactory;
import ar.fi.uba.fallas2.business.factories.GuessObjectFactory;
import ar.fi.uba.fallas2.business.factories.QuestionFactory;
import ar.fi.uba.fallas2.data.DataException;
import ar.fi.uba.fallas2.data.Database;

public class GenericFinder {
	public static int findLastObjectId() throws DataException {
		String findStatementString = "SELECT MAX(id_object) FROM object";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			rs = findStatement.executeQuery();
			if (rs.next()) return rs.getInt(1);
			else return -1;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public static GuessObject findGuessObjectById(int id) throws DataException {
		String findStatementString = "SELECT id_object, name FROM object WHERE id_object = ?";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			findStatement.setInt(1, id);
			rs = findStatement.executeQuery();
			if (rs.next()) return GuessObjectFactory.create(rs);
			else return null;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public static GuessObject findObjectByName(String name) throws DataException {
		String findStatementString = "SELECT id_object, name FROM object WHERE name = ?";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			findStatement.setString(1, name);
			rs = findStatement.executeQuery();
			if (rs.next()) return GuessObjectFactory.create(rs);
			else return null;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public static Question findQuestionById(int id) throws DataException {
		String findStatementString = "SELECT id_question, value FROM question WHERE id_question = ?";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			findStatement.setInt(1, id);
			rs = findStatement.executeQuery();
			if (rs.next()) return QuestionFactory.create(rs);
			else return null;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public static AnswerKind findKindById(int id) throws DataException {
		String findStatementString = "SELECT id_kind, value FROM answer_kind WHERE id_kind = ?";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			findStatement.setInt(1, id);
			rs = findStatement.executeQuery();
			if (rs.next()) return AnswerKindFactory.create(rs);
			else return null;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public static List<Answer> findAllAnswers() throws DataException {
		List<Answer> list = new ArrayList<Answer>();
		String findStatementString = "SELECT id_answer, fk_id_kind, fk_id_object, fk_id_question, weight FROM answer";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			rs = findStatement.executeQuery();
			while(rs.next()) {
				list.add(AnswerFactory.create(rs));
			}
			return list;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public static List<Question> findAllQuestions() throws DataException {
		List<Question> list = new ArrayList<Question>();
		String findStatementString = "SELECT id_question, value FROM question";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			rs = findStatement.executeQuery();
			while(rs.next()) {
				list.add(QuestionFactory.create(rs));
			}
			return list;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public static List<Answer> findAnswersByKind(AnswerKind kind) throws DataException {
		List<Answer> list = new ArrayList<Answer>();
		String findStatementString = "SELECT id_answer, fk_id_kind, fk_id_object, fk_id_question, weight FROM answer WHERE fk_id_kind = ?";
		PreparedStatement findStatement = null;
		ResultSet rs = null;
		try {
			findStatement = Database.getInstance().prepareStatement(findStatementString);
			findStatement.setInt(1, kind.getId());
			rs = findStatement.executeQuery();
			while(rs.next()) {
				list.add(AnswerFactory.create(rs));
			}
			return list;
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
}
